import { Component, OnInit, Input } from '@angular/core';
import { PersonnelService } from 'src/app/services/personnel.service';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { OrganizationService } from 'src/app/services/organization.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
@Component({
  selector: 'zepride-personnel-modal',
  templateUrl: './personnel-modal.component.html',
  styleUrls: ['./personnel-modal.component.less']
})
export class PersonnelModalComponent implements OnInit {
  @Input() organizationId = '';
  @Input() presentId = '';
  @Input() treeList;
  personnellistOfData = [];
  params = {
    name: '',
    phone: '',
    employeeNo: '',
    gender: '',
    beginTime: '',
    endTime: '',
    pageIndex: 1,
    countOfPage: 10
  }
  constructor(
    private personnelService: PersonnelService,
    private route: Router,
    private msg: NzMessageService,
    private modalRef: NzModalRef<PersonnelModalComponent>,
    private organizationService: OrganizationService
  ) { }

  ngOnInit(): void {
    this.personnelList();
  }

  //人员列表
  async personnelList() {
    try {
      const data = await this.personnelService.queryUsers(this.params);
      const list = data.list.map(item => {
        return {
          ...item,
          checked: false
        };
      });
      this.personnellistOfData = list;
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  clickskip(url: string) {
    this.route.navigate([url], { queryParams: { Id: 0, organizationId: this.organizationId, parentId: this.presentId } });
    this.cancel();
  }

  cancel() {
    this.modalRef.close();
  }

  confirm() {
    this.modalRef.close(true);
  }

  async updatePerson() {
    const params = [];
    this.treeList.nodes.forEach((t: any) => {
      this.personnellistOfData.forEach((p: any) => {
        if (t.objectId == p.id && p.checked == true) {
          params.push({ id: t.id, parentId: this.presentId });
        }
      })
    });
    if (params.length == 0) {
      this.msg.error('请选择人员');
      return;
    }
    try {
      await this.organizationService.updateorganizationTree(params);
      this.msg.success('添加人员成功');
      this.confirm();
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }

  }

}
